package com.company;

import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @date : 2024-06-21 下午12:31
 */

public class P072_ListRemoveAndAddTwice {


    public static void main(String[] args) {
        personListRemoveAndAddTwiceWithIterator();
    }


    /**
     * 满足条件
     */
    private static void personListRemoveTwiceAndAddTwiceWithReverseLoop() {
        List<Person> personList = new ArrayList<>();
        personList.add(new Person("张三", 20));
        personList.add(new Person("李四", 21));
        personList.add(new Person("王五", 22));

        System.out.println(personList);
        int size = personList.size();
        Person newPerson = new Person("张三", 20);
        for (int i = size - 1; i >= 0; i--) {
            Person person = personList.get(i);
            if (newPerson.getName().equals(person.getName()) && newPerson.getAge() == person.getAge()) {
                personList.remove(i);
            }
        }
        System.out.println(personList);
        personList.add(newPerson);

        System.out.println(personList);
        // 需要构建新的实体
        Person newPerson2 = new Person("王五", 22);
        size = personList.size();
        for (int i = size - 1; i >= 0; i--) {
            Person person = personList.get(i);
            if (newPerson2.getName().equals(person.getName()) && newPerson2.getAge() == person.getAge()) {
                personList.remove(i);
            }
        }
        System.out.println(personList);
        personList.add(newPerson2);
        System.out.println(personList);
    }


    /**
     * 满足条件
     */
    private static void personListRemoveTwiceAndAddTwiceWithStream() {
        List<Person> personList = new ArrayList<>();
        personList.add(new Person("张三", 20));
        personList.add(new Person("李四", 21));
        personList.add(new Person("王五", 22));
        System.out.println(personList);
        Person newPerson1 = new Person("张三", 20);
        personList = removeElement(personList, newPerson1);
        System.out.println(personList);
        personList.add(newPerson1);
        System.out.println(personList);
        // 需要构建新的实体
        Person newPerson2 = new Person("王五", 22);
        personList = removeElement(personList, newPerson2);
        System.out.println(personList);
        personList.add(newPerson2);
        System.out.println(personList);
    }

    private static List<Person> removeElement(List<Person> personList, Person newPerson) {
        return personList.stream().filter(person -> !(newPerson.getName().equals(person.getName()) && newPerson.getAge() == person.getAge())).collect(Collectors.toList());
    }


    /**
     * 满足条件
     */
    private static void personListRemoveAndAddTwiceWithIterator() {
        List<Person> personList = new ArrayList<>();
        personList.add(new Person("张三", 20));
        personList.add(new Person("李四", 21));
        personList.add(new Person("王五", 22));
        System.out.println(personList);
        Person newPerson1 = new Person("张三", 20);

        removeElementWithIterator(personList, newPerson1);

        System.out.println(personList);
        personList.add(newPerson1);
        System.out.println(personList);
        // 需要构建新的实体
        Person newPerson2 = new Person("王五", 22);
        removeElementWithIterator(personList, newPerson2);
        System.out.println(personList);
        personList.add(newPerson2);
        System.out.println(personList);
    }

    private static void removeElementWithIterator(List<Person> personList, Person newPerson) {
        Iterator<Person> iterator = personList.iterator();
        while (iterator.hasNext()) {
            Person person = iterator.next();
            if (newPerson.getName().equals(person.getName()) && newPerson.getAge() == person.getAge()) {
                iterator.remove();
            }
        }
    }


    /**
     * 满足条件
     */
    private static void personListRemoveAndAddTwiceWithRemoveIf() {
        List<Person> personList = new ArrayList<>();
        personList.add(new Person("张三", 20));
        personList.add(new Person("李四", 21));
        personList.add(new Person("王五", 22));
        System.out.println(personList);
        Person newPerson1 = new Person("张三", 20);
        removeElementWithRemoveIf(personList, newPerson1);
        System.out.println(personList);
        personList.add(newPerson1);
        System.out.println(personList);
        // 需要构建新的实体
        Person newPerson2 = new Person("王五", 22);
        removeElementWithRemoveIf(personList, newPerson2);
        System.out.println(personList);
        personList.add(newPerson2);
        System.out.println(personList);
    }

    private static void removeElementWithRemoveIf(List<Person> personList, Person newPerson) {
        personList.removeIf(person -> (newPerson.getName().equals(person.getName()) && newPerson.getAge() == person.getAge()));
    }


    @Data
    @AllArgsConstructor
    public static class Person {
        private String name;
        private int age;
    }
}
